﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Common.Client;
using Umbrella.Whs.Contracts;

namespace Umbrella.Whs.WhsAddin.SettingsControls
{
    public partial class UpdateStatus : SettingsControlBase
    {
        public UpdateStatus(UmbrellaSettingsTab tab)
            : base(tab)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Initialization start");

            try
            {
                InitializeComponent();
	
                this.updateAvailableLabel.Text = Properties.Resources.txtLoading;
	
                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Initialization done");
            }
            catch (Exception e)
            {
                _log.Fatal("Failed to initialize", e);

                throw;
            }
        }

        private void UpdateUpdateCheckStatus()
        {
// 			if (_log.IsDebugEnabled)
// 				_log.Debug("Updating update status...");

            try
            {
                var newVersion = _singletonService.Channel.GetNewerVersion();
                var lastCheck = _singletonService.Channel.GetLastUpdateCheck();

                if (newVersion != null)
                {
                    this.updateAvailableLabel.Text = string.Format(
                        Properties.Resources.txtUpdateAvailableYesMsg,
                        (lastCheck == DateTime.MinValue ? Properties.Resources.txtUpdateAvailableNeverChecked : lastCheck.ToString()),
                        newVersion.VersionString
                        );

                    this.statusImage.Image = CommonImages.StatusAtRisk24;
                }
                else
                {
                    this.updateAvailableLabel.Text = string.Format(
                        Properties.Resources.txtUpdateAvailableNoMsg,
                        (lastCheck == DateTime.MinValue ? Properties.Resources.txtUpdateAvailableNeverChecked : lastCheck.ToString()),
                        string.Empty
                        );

                    this.statusImage.Image = CommonImages.StatusHealthy24;
                }
	
            }
            catch (Exception ex)
            {
                _log.Error("Failed to update update status", ex);
            }
        }

        private void updateAvailableCheckTimer_Tick(object sender, EventArgs e)
        {
            if (_singletonService.IsWindowsServiceRunning() != SingletonServiceFactory.ServiceStatus.Running)
                return;

            UpdateUpdateCheckStatus();
        }

        private void updateAvailableCheckLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                _singletonService.Channel.CheckNewerVersionNow();

                UpdateUpdateCheckStatus();
            }
            catch (Exception ex)
            {
                _log.Error("Failed to check for update manually", ex);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    ex.Message,
                    Properties.Resources.txtManualUpdateSearchFailed,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void UpdateStatus_Load(object sender, EventArgs e)
        {
            UpdateUpdateCheckStatus();

            this.updateAvailableCheckTimer.Enabled = true;
        }

    }
}